//
//  ShoppingAddressController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/31.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "ShoppingAddressController.h"
#import "AddressModel.h"
#import "AddressNullView.h"
#import "AddNewAddressController.h"

@interface ShoppingAddressController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic , strong) NSMutableArray * dataArray;
@end

@implementation ShoppingAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}


-(void)setNavi
{
    self.title = @"收货地址";
}

-(void)setUI
{
    self.view.backgroundColor = Color(245, 245, 245);
    self.tableview.backgroundColor = Color(245, 245, 245);
    self.tableview.tableFooterView = [UIView new];
    self.dataArray = [NSMutableArray array];
    self.tableview.rowHeight = 140;
    [self.tableview registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:@"AdressCell"];
}

-(void)setData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@address/index",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            AddressNullView * view = [[[NSBundle mainBundle] loadNibNamed:@"AddressNullView" owner:self options:nil] lastObject];
            if ([[responseObject objectForKey:@"data"] count] == 0) {
                view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 119);
                [self.view addSubview:view];
            }else
            {
                [view removeFromSuperview];
                [self.dataArray removeAllObjects];
                for (int i = 0 ; i < [[responseObject objectForKey:@"data"] count]; i ++) {
                    AddressModel * model = [[AddressModel alloc]init];
                    [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"][i]];
                    [self.dataArray addObject:model];
                }
                [self.tableview reloadData];
            }
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AdressCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell bindDataWithModel:self.dataArray[indexPath.section]];
    cell.defaultBlock = ^{
        AddressModel * model = self.dataArray[indexPath.section];
        [self setAddressWithAddressID:model.address_id withUrl:@"address/set-default-address"];
    };
    cell.deleteBlock = ^{
        AddressModel * model = self.dataArray[indexPath.section];
        [self setAddressWithAddressID:model.address_id withUrl:@"address/delete-address"];
    };
    cell.editBlock = ^{
        AddressModel * model = self.dataArray[indexPath.section];
        [self editAddressWithID:model.address_id];
    };
    return cell;
}

- (void)editAddressWithID:(NSString *)modelID
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"address_id":modelID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@address/add-edit-address",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            AddressModel * model = [AddressModel new];
            [model setValuesForKeysWithDictionary:responseObject[@"data"]];
            AddNewAddressController * vc = [[AddNewAddressController alloc]init];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)setAddressWithAddressID:(NSString *)addressID withUrl:(NSString *)url
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"address_id":addressID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@%@",BaseUrl,url] para:dict success:^(BOOL isSuccess, id responseObject) {
        if (isSuccess) {
            [self setData];
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressModel * model = self.dataArray[indexPath.section];
    if (self.canClick) {
        self.block(model.address_id);
        [self.navigationController popViewControllerAnimated:YES];
    }else
    {
        return;
    }
}

- (IBAction)addNewAddress:(id)sender {
    AddNewAddressController * vc = [[AddNewAddressController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
